package me.lc.jokealot;

import java.util.ArrayList;
import java.util.HashMap;

import me.lc.jokealot.entity.JokeClassify;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.kuguo.ad.KuguoAdsManager;
import com.mobclick.android.MobclickAgent;

public class MainActivity extends Activity {
	private boolean isLoaded = false;
	public static MainActivity Instance;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		if (!isLoaded) {

			isLoaded = true;
			Instance = this;
			MobclickAgent.onError(this);
			AppControl.init();

			initJokeClass();
			initDataGrid();
			initUI();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	// Button btnQuit;

	private void initUI() {
		// btnQuit = (Button) findViewById(R.id.btnQuit);
		// btnQuit.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// showExitTips();
		// }
		// });

	}

	private void initDataGrid() {
		GridView gridview = (GridView) findViewById(R.id.gridview);
		ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();

		for (JokeClassify classify : DataManager.instance.classList) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			int pId = this.getResources().getIdentifier(
					"p" + classify.getClassId(), "drawable",
					this.getPackageName());
			map.put("classImage", pId);
			map.put("className", classify.getClassName());
			lstImageItem.add(map);
		}

		SimpleAdapter saImageItems = new SimpleAdapter(this, lstImageItem,
				R.layout.class_item,
				new String[] { "classImage", "className" }, new int[] {
						R.id.classImage, R.id.className });

		gridview.setAdapter(saImageItems);
		gridview.setOnItemClickListener(new ItemClickListener());

	}

	class ItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			DataManager.instance.currentCategoryIndex = DataManager.instance.classList
					.get(arg2).getClassId();
			DataManager.instance.currentCategoryName = DataManager.instance.classList
					.get(arg2).getClassName();
			DataManager.instance.loadJokeList(
					DataManager.instance.currentCategoryIndex,
					MainActivity.this);

			// 上次记录的index
			int lastIndex = DataManager.instance.getLastJokeIndex();
			Intent intent = new Intent(MainActivity.this, JokeActivity.class);
			intent.putExtra("index", lastIndex);
			MainActivity.this.startActivity(intent);
		}

	}

	/**
	 * 初始化笑话列表
	 */
	private void initJokeClass() {
		DataManager.instance.initData(this);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			showExitTips();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void showExitTips() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				AlertDialog alertDialog = new AlertDialog.Builder(
						MainActivity.this)
						.setTitle("退出")
						.setMessage("是否退出笑话乐翻天？")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										// 酷果
										KuguoAdsManager paManager = KuguoAdsManager
												.getInstance();
										paManager
												.setCooId(
														MainActivity.Instance,
														"866a143a367242d79651bf43d10f0615");
										paManager.receivePushMessage(
												MainActivity.Instance, true);
										MainActivity.this.finish();
									}
								})
						.setNegativeButton("取消",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										return;
									}
								}).setCancelable(false).create(); // 创建对话框
				alertDialog.show();
			}
		});
	}
}